package cn.wolfcode.rbac.service.impl;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.domain.Role;
import cn.wolfcode.rbac.mapper.PermissionMapper;
import cn.wolfcode.rbac.query.PageResult;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.util.RequriedPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javafx.application.Application;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ApplicationContext ctx;

    //load()加载实现

    @Override
    @Transactional
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> list = this.permissionMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(Permission entity) {
        if (entity.getId() == null) {
            this.permissionMapper.insert(entity);
        } else {
            this.permissionMapper.updateByPrimaryKey(entity);
        }
    }

    @Override
    public void delete(Long paramLong) {
        this.permissionMapper.deleteByPrimaryKey(paramLong);
    }

    @Override
    @Transactional(readOnly = true)
    public Permission get(Long id) {
        return this.permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Permission> list() {
        return this.permissionMapper.selectAll();
    }

    @Override
    public void load() {
        //比较
        //第一步，获取权限表中的所有权限记录
        List<String> permissions = this.permissionMapper.selectAllExpression();
        //通过上下文来获取用户的权限，并保持到集合中
        Map<String, Object> map = this.ctx.getBeansWithAnnotation(Controller.class);
        Collection<Object> values = map.values();
        for (Object controller : values) {
            Class<?> clazz = controller.getClass();
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                RequriedPermission annotation = (RequriedPermission) method.getAnnotation(RequriedPermission.class);
                if (annotation != null) {
                    String name = annotation.name();
                    String simpleName = clazz.getSimpleName().replace("Controller", "");
                    //获取该方法中有自定义注解的表达式值
                    String expeoession = StringUtils.uncapitalize(simpleName) + ":" + method.getName();
                    //开始对比，将对比后的信息保存
                    if (!permissions.contains(expeoession)) {
                        Permission permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expeoession);
                        this.permissionMapper.insert(permission);
                    }
                }
            }
        }

    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return this.permissionMapper.selectByEmpId(id);
    }


}
